<template>
  <div class="login-pwd">
    <h3 class="login__title">
      <img src="@/assets/images/logo_icon.png" alt="" />
      <span>{{ $t("login.login.title") }}</span>
    </h3>
    <div class="login__type">
      <div :class="{ 'type--active': loginType == 1 }" @click="loginType = 1">
        {{ $t("login.login.loginType.account") }}
      </div>
      <div :class="{ 'type--active': loginType == 2 }" @click="loginType = 2">
        {{ $t("login.login.loginType.sms") }}
      </div>
    </div>
    <el-form
      :rules="rules"
      ref="ruleForm"
      :model="useForm"
      inline-message
      @validate="validate"
    >
      <el-form-item prop="mobile">
        <el-input
          v-model="useForm.mobile"
          class="w-50 m-2"
          size="large"
          :placeholder="$t('placeholder.mobile')"
        >
          <template #prefix>
            <i class="iconfont ic-worker-fill"></i>
          </template>
          <template #suffix>
            <i
              v-show="formIcon.mobile"
              :class="`iconfont ${formIcon.mobile}`"
            ></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password" v-if="loginType == 1">
        <el-input
          v-model="useForm.password"
          class="w-50 m-2"
          size="large"
          :placeholder="$t('placeholder.password')"
          show-password
          type="password"
        >
          <template #prefix>
            <i class="iconfont ic-lock"></i>
          </template>
          <template #suffix>
            <i
              v-show="formIcon.password"
              :class="`iconfont ${formIcon.password}`"
            ></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="loginType == 2">
        <el-input
          v-model="useForm.code"
          class="w-50 m-2"
          size="large"
          @input="validateNum()"
          maxlength="6"
          :placeholder="$t('placeholder.code')"
        >
          <template #suffix>
            <i v-show="formIcon.code" :class="`iconfont ${formIcon.code}`"></i>
          </template>
          <template #append>
            <div
              class="code get-code"
              v-if="!codeEvent.flag"
              @click="
                codeEvent.getCode({
                  phoneNumber: useForm.mobile,
                  type: 'captcha_login',
                })
              "
            >
              {{ $t("verCode.getCode") }}
            </div>
            <div class="code" v-else>
              {{ $t("verCode.resend") }}{{ codeEvent.time }}S
            </div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <div
          class="login__btn"
          :class="{ 'login__btn--active': submitBtn }"
          @click="submit()"
        >
          登录
        </div>
      </el-form-item>
    </el-form>
    <el-checkbox
      v-model="useForm.autoLogin"
      :label="$t('login.login.autoLogin')"
      size="small"
      v-if="loginType == 1"
    ></el-checkbox>
    <div class="login__forget-pwd">{{ $t("login.login.forgetPwd") }}？</div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from "vue";
import validateForm from "./composables/validateForm";
import getUserInfo from "./composables/getUserInfo";
import verificationCode from "@/composables/verificationCode";
const { codeEvent } = verificationCode();
const { useLogin, useForm } = getUserInfo();

const {
  rules,
  submit,
  ruleForm,
  validate,
  formIcon,
  submitBtn,
  loginType,
  validateNum,
} = validateForm(useLogin, useForm);
</script>
<style lang="scss" scoped>
.login-pwd {
  width: 520px;
  height: 550px;
  border-radius: 8px;
  padding: 64px 80px 40px;
  --el-color-primary: #6042dc;
}
.login {
  &__title {
    font-size: 32px;
    margin-bottom: 58px;
    @include flex-y-center;
    img {
      margin-right: 16px;
      width: 40px;
      height: 40px;
    }
  }
  &__type {
    display: flex;
    margin-bottom: 24px;
    align-items: baseline;
    div {
      margin-right: 24px;
      color: map-get($font-sizes, text);
      font-size: 16px;
      cursor: pointer;
    }
    .type {
      &--active {
        font-size: 18px;
        color: map-get($font-sizes, title);
      }
    }
  }

  &__btn {
    width: 100%;
    height: 40px;
    line-height: 40px;
    text-align: center;
    border-radius: 2px;
    color: map-get($font-sizes, white);
    background: map-get($bg-primary, disabled);
  }
  &__btn--active {
    background: map-get($bg-primary, active);
    cursor: pointer;
  }
  &__forget-pwd {
    width: 100%;
    color: map-get($font-sizes, smTitle);
    text-align: center;
    @include absolute-left-bottom-0();
    bottom: 40px;
  }
}
:deep(.el-form-item) {
  --el-color-white: #f2f3f5;
  margin-bottom: 16px;
  .el-input__inner {
    border: none;
    color: map-get($font-sizes, title);
  }
  .el-input__prefix {
    left: 12px;
  }
  .iconfont {
    margin-right: 5px;
  }
  i {
    height: 40px;
    line-height: 40px;
    color: map-get($font-sizes, text);
  }
  input[type="password"]::-ms-reveal {
    display: none;
  }
  .el-input__validateIcon {
    display: none;
  }
  .ic-circle-correct-fill {
    color: map-get($map: $status-success, $key: static);
  }
  .ic-circle-wrong-fill {
    color: #f53f3f;
  }
  .code {
    width: 84px;
    height: 100%;
    text-align: center;
    position: relative;
    &::before {
      content: "";
      display: inline-block;
      width: 1px;
      height: 20px;
      position: absolute;
      left: -15px;
      top: 50%;
      margin-top: -10px;
      background: map-get($map: $fill-color, $key: weight);
    }
  }
  .get-code {
    cursor: pointer;
  }
  .el-input-group__append {
    border: none;
  }
}
</style>
